package com.example.ymm.modules1.service.impl.user;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.OfficeChangeLog;
import com.example.ymm.modules1.entity.my.user.OfficeUser;
import com.example.ymm.modules1.mapper.my.user.OfficeChangeLogMapper;
import com.example.ymm.modules1.query.OfficeChangeLogQueryCriteria;
import com.example.ymm.modules1.service.dd.DingOaService;
import com.example.ymm.modules1.service.user.OfficeChangeLogService;
import com.example.ymm.modules1.service.user.OfficeUserService;
import com.example.ymm.util.DateKit;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class OfficeChangeLogServiceImpl extends BaseServiceImpl<OfficeChangeLogMapper, OfficeChangeLog> implements OfficeChangeLogService {

    @Autowired
    DingOaService dingOaService;
    @Autowired
    OfficeUserService officeUserService;


    @Override
    public TableList<OfficeChangeLog> list(OfficeChangeLogQueryCriteria criteria, YmPageDto pageDto) {
        IPage<OfficeChangeLog> page = new Page<OfficeChangeLog>(pageDto.getPage(),pageDto.getPageSize());
        baseMapper.selectPage(page, QueryHelpPlus.getPredicate(OfficeChangeLog.class, criteria));
        TableList<OfficeChangeLog> tableList=new TableList(page);
        return tableList;
    }

    public List<OfficeChangeLog> queryAll(OfficeChangeLogQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(OfficeChangeLog.class, criteria);
        predicate.orderByDesc("create_time");
        List<OfficeChangeLog> result =baseMapper.selectList(predicate);
        return result;
    }


    @Override
    public void scheduledAdd() {
        JSONObject json = dingOaService.getProcessListIds();
        if(json!=null&&!json.isEmpty()){
            JSONArray arr = json.getJSONArray("list");
            for(int i=0;i<arr.size();i++){
                String id = arr.getString(i);
                OfficeChangeLog changeLog = getById(id);
                if(UtilValidate.isEmpty(changeLog)){
                    JSONObject processDetail = dingOaService.getProcessDetail(id);
                    if(UtilValidate.isNotEmpty(processDetail)){
                        String user_id = processDetail.getString("originator_userid");
                        OfficeUser officeUser = officeUserService.findByUserId(user_id);
                        changeLog=new OfficeChangeLog();
                        changeLog.setUserId(user_id);
                        if(UtilValidate.isNotEmpty(officeUser)){
                            changeLog.setUserName(officeUser.getUserName());
                            officeUser.setIzChange(1);
                            officeUserService.saveOrUpdate(officeUser);
                        }
                        changeLog.setTitle(processDetail.getString("title"));
                        changeLog.setApproveNo(processDetail.getString("business_id"));
                        String times = processDetail.getString("create_time");
                        Date date = DateKit.dateFormat(times);
                        changeLog.setAddTime(new Timestamp(date.getTime()));
                        changeLog.setTxtJson(processDetail.toJSONString());
                        saveOrUpdate(changeLog);

                    }
                }
            }
        }
    }
}
